<?php
/**
 * Created by PhpStorm.
 * User: XZR
 * Date: 2018/8/1
 * Time: 15:21
 */

namespace Api\Controller;


//病历详情控制器

use Api\Model\MedicalRecordDetailModel;
use Api\Model\MedicalRecordModel;
use Api\Model\UserModel;
use Common\Util\ConfigUtils;
use Think\Upload;

class RecordDetailController extends AuthController
{

    public function get(){
        $this->validateParam(array("id"));
        $id  =$this->getPostParam("id");
        $row =MedicalRecordDetailModel::singleton()->getOneById($id);
        $rst['result']=200;
        $rst['info']=$row;
        $this->formatjson($rst);
    }

    //添加病历资料基础信息
    public function create(){
        $data= $this->json();
        $record_id= $data['record_id'];
        $id= $data['id'];
        if(!$record_id>0){
            $rst['msg']="record_id参数不对";
            $rst['result']=-2;
            $this->formatjson($rst);
        }
        $rst = MedicalRecordDetailModel::singleton()->addOrUpdate($record_id,$id,$data['data']);
        $this->formatjson($rst);
    }

    //添加病历资料图片
    public function updateImgage(){
        $data= $this->json();
        $id= $data['id'];
        if(!$id>0){
            $rst['msg']="id参数不对";
            $rst['result']=-2;
            $this->formatjson($rst);
        }
        $res = MedicalRecordDetailModel::singleton()->updateImg($id,$data['data']);
        if ($res !== FALSE) {
            $rst['result']=200;
            $rst['msg']='修改成功';
        }else{
            $rst['result']=201;
            $rst['msg']='修改失败';
        }
        $this->formatjson($rst);
    }

    //获取病历资料图片
    public function getImage(){
        $this->validateParam(array("id"));
        $id= $this->getPostParam('id');
        $res = MedicalRecordDetailModel::singleton()->getImage($id);
        $rst['result']=200;
        $rst['info']=$res;
        $this->formatjson($rst);
    }


    public function uploadImage(){

        $config = array(
            'maxSize'       =>  0, //上传的文件大小限制 (0-不做限制)
            'exts'          =>  array('jpg','png','gif','jpeg'), //允许上传的文件后缀
            'rootPath'      =>  './Upload/', //保存根路径
            'driver'        =>  'LOCAL', // 文件上传驱动
            'subName'       =>  array('date', 'Y-m-d'),
            'savePath'      =>  ''
        );
        $upload = new \Think\Upload($config);
        $rs = $upload->upload($_FILES);
        $Filedata = key($_FILES);
        if(!$rs){
            $data['status'] = -1;
            $data['msg'] = "上传图片失败";
        }else{
            $url = ConfigUtils::getConfig('SERVER_URL');
            $data['imgUrl'] =$url."Upload/".$rs[$Filedata]['savepath'].$rs[$Filedata]['savename'];
            $data['status'] = 200;
        }

        $this->formatjson($data);


    }

    public function delete(){
        $this->validateParam(array("id"));//病历资料id
        $id = $this->getPostParam("id", 0);
        $rst = MedicalRecordDetailModel::singleton()->deleteRecordDetail($id);

        $this->formatjson($rst);
    }
}